import numpy as np
from sklearn.model_selection import train_test_split
import pandas as pd
import scipy as sp
from sklearn.linear_model import LinearRegression,RidgeCV,LassoCV
from sklearn.metrics import mean_squared_error


df = pd.read_csv('FE_day.csv')
features = df.columns[0:df.columns.size - 1]
data = df[features]
target = df[df.columns[df.columns.size - 1]]

feat_names=data.columns

data_train, data_test, target_train, target_test = train_test_split(data, target, test_size=0.2)



'''开方均方误差'''
def rmse(y,pre_y):
    return

'''最小二乘线性回归模型'''
lr=LinearRegression(fit_intercept=True)
lr_fit = lr.fit(data_train,target_train)
lr_pre=lr.predict(data_test)
'''看看各特征的权重系数，系数绝对值大小可视为该特征的重要性'''
fs=pd.DataFrame({"columns":list(feat_names),"coef":list((lr.coef_.T))})
fs=fs.sort_values(by=['coef'],ascending=False)
print(fs.head(n=feat_names.size))

rmse_value = sp.sqrt(mean_squared_error(target_test, lr_pre))

print("最小二乘线性回归模型的RMSE:{}".format(rmse_value))
print("最小二乘线性回归模型的系数:{}".format(lr.coef_))
print("最小二乘线性回归模型的截距:{}".format(lr.intercept_))

'''岭回归模型'''
'''
1.设置正则参数范围
'''
alphas=[0.001,0.01,1,10,100,1000]
'''
2.生成一个RidgeCv实例
'''
ridge= RidgeCV(alphas=alphas,store_cv_values=True)
'''
3.模型训练
'''
ridge.fit(data_train,target_train)

alpha=ridge.alpha_
print("岭回归模型最好的appha:{}".format(alpha))
'''
交叉验证估计的测试误差
'''
mse_cv=np.mean(ridge.cv_values_,axis=0)
rmse_cv=np.sqrt(mse_cv)
print("岭回归模型RMSE的CV:{}".format(rmse_cv))

'''
4.预测
'''
'''
训练上测试，训练误差
'''
target_train_pre=ridge.predict(data_train)
rmse_train=np.sqrt(mean_squared_error(target_train,target_train_pre))

target_test_pre=ridge.predict(data_test)
rmse_test=np.sqrt(mean_squared_error(target_test,target_test_pre))

print('岭回归模型在训练集上的RMSE:{}'.format(rmse_train))
print('岭回归模型在测试集上的RMSE:{}'.format(rmse_test))
print("岭回归模型模型的系数:{}".format(ridge.coef_))
print("岭回归模型模型的截距:{}".format(ridge.intercept_))


'''Lasso模型'''
'''
1.生成LassoCV实例
'''
lasso=LassoCV()

'''
2.训练
'''
lasso.fit(data_train,target_train)
alpha=lasso.alpha_
print("Lasso模型最好的alpha:{}".format(alpha))
'''
3.预测
'''
target_train_pre=lasso.predict(data_train)
target_test_pre=lasso.predict(data_test)

rmse_train=np.sqrt(mean_squared_error(target_train,target_train_pre))
rmse_test=np.sqrt(mean_squared_error(target_test,target_test_pre))
print("Lasso模型在训练集上的RMSE:{}".format(rmse_train))
print("Lasso模型在测试集上的RMSE:{}".format(rmse_test))
print("Lasso模型的系数:{}".format(lasso.coef_))
print("Lasso模型的截距:{}".format(lasso.intercept_))













